
package com.googlecode.esblabs.util.mule.transformer.jaxb;

import java.io.StringWriter;

import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import org.mule.api.MuleMessage;
import org.mule.api.transformer.TransformerException;

public class JaxbObjectToXml extends AbstractJaxbTransformer
{
    public JaxbObjectToXml()
    {
        registerSourceType(Object.class);
        setReturnClass(String.class);
    }

    public Object transform(MuleMessage message, String outputEncoding) throws TransformerException
    {
        Object src = message.getPayload();
        try
        {
            Marshaller unmarshaller = jaxbContext.createMarshaller();
            StringWriter writer = new StringWriter();
            unmarshaller.marshal(src, writer);

            if (logger.isDebugEnabled()) logger.debug("transform(" + src.getClass().getSimpleName() + ", " + outputEncoding + ") returns: " +  writer.toString());
            
            return writer.toString();
        }
        catch (JAXBException e)
        {
            throw new TransformerException(this, e);
        }

    }
}
